package core

import (
	"base-web/global"
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

const defaultConfigFile = "./config/config.yaml"

//初始化配置
func init() {
	v := viper.New()
	v.SetConfigFile(defaultConfigFile)
	err := v.ReadInConfig()
	if err != nil {
		panic(err)
	}
	v.WatchConfig()
	v.OnConfigChange(func(in fsnotify.Event) {
		fmt.Println("config file change:", in.Name)
		if err := v.Unmarshal(&global.BASE_CONFIG); err != nil {
			fmt.Println(err)
		}
	})

	if err := v.Unmarshal(&global.BASE_CONFIG); err != nil {
		fmt.Println(err)
	}

	fmt.Println("初始化文件配置成功！")
}
